/*
 * merghashcode.cpp
 *
 *  Created on: 2011-10-11
 *      Author: shenjiong
 */

#include "merghashcode.h"

int basePrimeNumber = 999983;

static int hashCode(const std::string & m);
static int getUnsignedInteger(int input);
static int getUnsignedInteger(int input, int unsignedMax);

long long gethashcode(const std::string & m)
{
    return getUnsignedInteger(hashCode(m));
    
}

static int hashCode(const std::string & m)
{
    
    int hash = 0;
    int h = hash;
    int offset = 0;
    long long count = m.length();
    
    if(h == 0 && count > 0)
    {
        int off = offset;
        const char *val = m.c_str();
        int len = count;
        
        for(long long i = 0; i < len; i++)
        {
            h = 31 * h + val[off++];
        }
        hash = h;
    }
    return h;
}

static int getUnsignedInteger(int input)
{
    return getUnsignedInteger(input, basePrimeNumber);
}

static int getUnsignedInteger(int input, int unsignedMax)
{
    int i = input % unsignedMax;
    if(i < 0)
    {
        i = 0 - i;
    }
    return i;
}
